using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class TemplateControl : BaseItem
    {
        private TemplateControl() : base() { }
        private TemplateControl(Hashtable table) : base(table) { }
        private TemplateControl(Hashtable table, List<TemplateControl> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TemplateControlDal();
        }
        
        public static TemplateControl CreateInstance()
        {
            return new TemplateControl();
        }

        private static TemplateControl GetItem(Hashtable table)
        {
            if (table != null)
                return new TemplateControl(table);
            else
                return null;
        }

        private static List<TemplateControl> GetItems(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<TemplateControl> tmp = new List<TemplateControl>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new TemplateControl(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static TemplateControl GetById(int id)
        {
            TemplateControlDal dal = new TemplateControlDal();
            return GetItem(dal.GetById(id));
        }

        public static List<TemplateControl> GetAllByTemplateId(int templateId)
        {
            TemplateControlDal dal = new TemplateControlDal();
            return GetItems(dal.GetAllByTemplateId(templateId));
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[TemplateControlDal.Fields.Id];
            templateId = (int)table[TemplateControlDal.Fields.TemplateId];
            name = (string)table[TemplateControlDal.Fields.Name];
            typeId = (int)table[TemplateControlDal.Fields.TypeId];
            typeName = (string)table[TemplateControlDal.Fields.TypeName];
            adminControlType = (string)table[PostControlDal.Fields.AdminControlType];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int templateId;
        public int TemplateId
        {
            get { return templateId; }
            set { templateId = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        
        private int typeId;
        public int TypeId
        {
            get { return typeId; }
            set { typeId = value; }
        }

        private string typeName;
        public string TypeName
        {
            get { return typeName; }
        }

        private string adminControlType;
        public string AdminControlType
        {
            get { return adminControlType; }
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((TemplateControlDal)dal).Update(ref id, templateId, name, typeId, ref typeName, ref adminControlType, ref concurencyId);
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateControlDal)dal).Delete(id, concurencyId);
        }
    }
}
